package cn.geracomium.action;


import cn.geracomium.util.action.AbstractAction;
import cn.geracomium.util.web.upload.FdfsUtil;
import cn.gov.service.IAccountManageService;
import cn.gov.service.IAuthService;
import cn.gov.service.IStationService;
import cn.gov.vo.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pages/account/")

public class AccountAddAction extends AbstractAction {
    @Reference
    private IAccountManageService accountManageService;
    @Reference
    private IStationService stationService ;
    @Reference
    private IAuthService authService ;
    @Value("${pic.server.url}")
    private String fdfsURL;
    @RequestMapping("add_pre")
    public ModelAndView addPre() {
        ModelAndView mav = new ModelAndView("pages/files/account/account_add");
        mav.addObject("roles",authService.list());
        return mav;
    }

    @PostMapping("empPortraitUpload")
    @ResponseBody
    public Map upload(HttpServletRequest request, MultipartFile myFile) {
        Map<String,String> result = new HashMap<>() ;
        try {
            String url = this.fdfsURL + "/" + FdfsUtil.upload(myFile)[0] + "/" + FdfsUtil.upload(myFile)[1];
            result.put("url",url) ;
            result.put("msg","成功") ;

        } catch (Exception e) {
            e.printStackTrace();
            result.put("err","上传失败") ;
            return result;
        }
        return result;
    }

    @PostMapping("add")
    @ResponseBody
    public Object add(PersonalYun personalYun, Employee employee,Integer roleId) {
        boolean flag = false ;
        System.out.println(personalYun);
        System.out.println(employee);
        System.out.println(roleId);
        try {

            System.out.println(personalYun);
            Long accountId = accountManageService.add(personalYun, employee);
            flag = true ;

            System.out.println(accountId + "传递回来的id是");
            PersonalServiceStation pss = new PersonalServiceStation() ;
            pss.setAccountId(accountId.toString());
            pss.setServerStationCode(employee.getServerStationCode());
            pss.setCreateTime(new Date());
            PersonRole pr = new PersonRole() ;
            pr.setAccountId(accountId);
            pr.setRoleId(roleId);
            pr.setCreateTime(new Date());
            flag = authService.addPersonStationAndRole(pss,pr) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    @PostMapping("list_station")
    @ResponseBody
    public Object listStation(Station station) {

        return stationService.findCondition(station) ;

    }



    @RequestMapping("error")
    public String error() {
        return "pages/files/account/errors" ;
    }
}
